package in.muton.edushare.components.messaging.api;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.auth.api.Authentication;
import in.muton.edushare.components.messaging.util.EscapeHTML;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.dao.OutgoingEmailsDAO;
import com.edushare.common.vo.Users;

public class UserMessage extends Message {

	public UserMessage(String message) {
		if(message.contains("<<<EDUSHAREMESSAGINGMETADATA")){
			int start = message.indexOf("<<<EDUSHAREMESSAGINGMETADATA");
			String temp = message.substring(0,start);
			temp += "<<EDUSHAREMESSAGINGMETADATA";
			temp += message.substring(message.indexOf("<<<EDUSHAREMESSAGINGMETADATA") + 
					"<<<EDUSHAREMESSAGINGMETADATA".length());
			message = temp;
		}
		this.message = EscapeHTML.processInputString(message);
		log.debug("Message is"+message);
	}

	@Override
	protected String getMessage() {
		return message;
	}
	
	public static boolean isValidMessage(String str){
		if(str.length()>ApplicationConstants.MAX_MSG_SIZE)
			return false;
		return true;
	}

	private String message = "";

	@Override
	protected void addditionalProcessing(Users u){
		Users loggedIn = EduShareSession.get().getLoggedInUser();
		log.debug("Doing additional processing");
		Authentication auth = new Authentication(u, EduShareSession.get().getLoggedInUser());
		if(auth.getSetting(Authentication.NOTIFY_MSG).equals("t") && u!=loggedIn){
			log.debug("Doing Actually sending message");
			new OutgoingEmailsDAO(EduShareSession.get().getSessionORMInterface()).sendMail(
				new com.edushare.common.mailer.Message(u, loggedIn, message).send());
		}
	}
}
